//
//  WPCLeftSidebarPresenter.swift
//  Wallpaper.co
//
//  Created by zhou_yuepeng on 2017/9/25.
//  Copyright © 2017年 www.dahuatech.com. All rights reserved.
//

import UIKit

typealias refreshBlock = (Bool) -> Void;

class WPCLeftSidebarPresenter: NSObject {
    weak var homepage:WPCHomepageViewController?;
    private weak var controller:WPCLeftSidebarViewController?;
    private var bannerModel:WPCBannerModel?;
    private var categoryModel:NSMutableArray?;
    
    var sectionCount:Int {
        get {
            return ((categoryModel == nil) ? 0 : (categoryModel?.count)!);
        }
    }
    
    func attachController(ctler: WPCLeftSidebarViewController) {
        controller = ctler;
    }
    
    func refreshBannerModel(rBlock:refreshBlock!){
        if bannerModel != nil {
            return;
        }
        
        WPCNetWorkingManager.shareInstance().queryGeneralInfo(withSource: "banner", model: WPCBannerModel.classForCoder(), success: { (modelObj:Any) in
            self.bannerModel = modelObj as? WPCBannerModel;
            
            rBlock(true);
        }) { (error) in
            rBlock(false);
        }
    }
    
    func refreshCategoryModel(rBlock:refreshBlock!){
        if categoryModel != nil {
            return;
        }
        
        WPCNetWorkingManager.shareInstance().queryGeneralInfo(withSource: "category", model: WPCCategoryModel.self, success: { (modelObj:Any) in
            let categoryArrays = NSMutableArray();
            let tempModel = modelObj as? WPCCategoryModel;
            
            let new = WPCCategoryItem();
            new.tag = "new";
            new.title = "New";
            
            let best = WPCCategoryItem();
            best.tag = "daily-best";
            best.title = "Best";
            
            let favorites = WPCCategoryItem();
            favorites.tag = "favorites";
            favorites.title = "Favorites";
            
            let items:NSMutableArray = ((tempModel?.result)!);
            items.insert(favorites, at: 0);
            items.insert(best, at: 0);
            items.insert(new, at: 0);
            
            tempModel?.result = items;
            tempModel?.totalcount += 3;
            
            categoryArrays.add(tempModel!);
            
            WPCNetWorkingManager.shareInstance().queryGeneralInfo(withSource: "hot", model: WPCCategoryModel.self, success: { (modelObj:Any) in
                categoryArrays.add(modelObj);
                
                self.categoryModel = categoryArrays;
                rBlock(true);
            }, failure: { (error) in
                rBlock(false);
            })
            
        }) { (error) in
            rBlock(false);
        }
    }
    
    func getBannersArray() -> NSMutableArray {
        let banners = NSMutableArray();
        for item in (bannerModel?.result)! {
            banners.add((item as! WPCBannerItem).imgurl);
        }
        
        return banners;
    }
    
    func getCategoryItemWith(section: Int, index: Int) -> WPCCategoryItem? {
        if nil == categoryModel || section >= (categoryModel?.count)! {
            return nil;
        }
        
        let model:WPCCategoryModel = categoryModel?.object(at: section) as! WPCCategoryModel;
        if index >= model.totalcount {
            return nil;
        }
        
        return model.result.object(at: index) as? WPCCategoryItem;
    }
    
    func numberOfItemsIn(section: Int) -> Int {
        if nil == categoryModel || section >= (categoryModel?.count)! {
            return 0;
        }
        
        let model:WPCCategoryModel = categoryModel?.object(at: section) as! WPCCategoryModel;
        return model.totalcount;
    }
    
    func didSelectCategoryItem(indexPath:IndexPath) {
        let model:WPCCategoryItem = getCategoryItemWith(section: indexPath.section, index: indexPath.row)!;
        homepage?.configController(withTag: model.tag, title: model.title);
    }
}
